import Vue from 'vue';
import Home from '../views/Home.vue';
import VueRouter from 'vue-router'
import Router from 'vue-router';
//路由导航冗余报错（路由重复）
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

Vue.use(VueRouter)

const routes = [{
    path: '/',
    name: 'Home',
    component: Home,
    meta: {
      title: "首页",
      requiresAuth: true
    }
  },
  {
    path: '/community',
    name: 'Community',
    component: () => import('../views/Community.vue'),
    meta: {
      title: "社区",
      requiresAuth: true
    }
  },
  {
    path: '/',
    name: 'Home',
    component: Home,
    meta: {
      title: "首页",
      requiresAuth: true
    }
  },
  {
    path: '/my',
    name: 'My',
    component: () => import('../views/My.vue'),
    meta: {
      title: "我的",
      requiresAuth: true
    },
  },
  {
    path: '/community',
    name: 'Community',
    component: () => import('../views/Community.vue'),
    meta: {
      title: "社区",
      requiresAuth: true
    },
  },
  // {
  //   path: '/complaint',
  //   name: 'Complaint',
  //   component: () => import('../views/Complaint.vue'),
  //   meta: {
  //     title: "投诉服务",
  //     requiresAuth: true
  //   },
  // },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue'),
    meta: {
      title: "登录",
      requiresAuth: true
    },
  },
  {
    path: '/my',
    name: 'My',
    component: () => import('../views/My.vue'),
    meta: {
      title: "我的",
    }
  },
  {
    path: '/repair',
    name: 'Repair',
    component: () => import('../views/Repair.vue'),
    meta: {
      title: "报修服务",
      requiresAuth: true
    }
  },
  {
    path: '/complain',
    name: 'Complain',
    component: () => import('../views/Complain.vue'),
    meta: {
      title: "投诉服务",
      requiresAuth: true
    },
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue'),
    meta: {
      title: "登录/注册",
      requiresAuth: true
    },
  },
  {
    path: '/staff',
    name: 'Staff',
    component: () => import('../views/Staff.vue'),
    meta: {
      title: "人事部门",
      requiresAuth: true
    },
  },
  {
    path: '/details',
    name: 'Details',
    component: () => import('../views/Details.vue'),
    meta: {
      title: "详情页",
    }
  },
  {
    path: '/myHouse',
    name: 'MyHouse',
    component: () => import('../views/MyHouse.vue'),
    meta: {
      title: "我的房屋",
      requiresAuth: true
    }
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('../views/Register.vue'),
    meta: {
      title: "注册",
      requiresAuth: true
    },
  },
  {
    path: '/visitorGo',
    name: 'VisitorGo',
    component: () => import('../views/visitorGo.vue'),
    meta: {
      title: "访客通行",
      requiresAuth: false
    }
  },
  {
    path: '/headlineDetail',
    name: 'HeadlineDetail',
    component: () => import('../views/HeadlineDetail.vue'),
    meta: {
      title: "物业公告详情",
      requiresAuth: false
    },
  },
  {
    path: '/hotspotDetail',
    name: 'HotspotDetail',
    component: () => import('../views/HotspotDetail.vue'),
    meta: {
      title: "热点新闻详情",
      requiresAuth: false
    },
  },
  {
    path: '/visitorGo',
    name: 'VisitorGo',
    component: () => import('../views/visitorGo.vue'),
    meta: {
      title: "访客通行",
      requiresAuth: false
    }
  },


  {
    path: '/expressage',
    name: 'Expressage',
    component: () => import('../views/Expressage.vue'),
    meta: {
      title: "我的快递",
      requiresAuth: false
    }
  },

  {
    path: '/red',
    name: 'Red',
    component: () => import('../views/Red.vue'),
    meta: {
      title: "开门红",
      requiresAuth: true
    }
  },
  {
    path: '/myHouse',
    name: 'MyHouse',
    component: () => import('../views/MyHouse.vue'),
    meta: {
      title: "我的房屋",
      requiresAuth: true
    },
  },
  {
    path: '/headlines',
    name: 'Headlines',
    component: () => import('../views/Headlines.vue'),
    meta: {
      title: "小区头条",
      requiresAuth: false
    },
  },
  {
    path: '/headlineDetail',
    name: 'HeadlineDetail',
    component: () => import('../views/HeadlineDetail.vue'),
    meta: {
      title: "小区公告",
      requiresAuth: false
    },
  },
  {
    path: '/hotspotDetail',
    name: 'HotspotDetail',
    component: () => import('../views/HotspotDetail.vue'),
    meta: {
      title: "小区公告",
      requiresAuth: false
    },
  },
  {
    path: "/my_news",
    name: "/My_news",
    component: () => import('../views/my_news'),
    meta: {
      title: "消息中心",
      requiresAuth: false
    }
  },
  {
    path: "/Eachcenter",
    name: "/Eachcenter",
    component: () => import('../views/Eachcenter'),
    meta: {
      title: "个人资料",
      requiresAuth: false
    }
  }
]
// {
//   path: '/search',
//   name: 'Search',
//   component: () => import('../views/Search.vue'),
//   meta: {
//     title: "搜索",
//     requiresAuth: true
//   },
// },
const router = new VueRouter({
  routes
})

// 路由前置守卫
router.beforeEach((to, from, next) => {
  // 取出存储的用户登录数据
  var user = localStorage.getItem("Owner");
  // console.log(user);
  // 不用登录的页面
  if (to.path == "/") {
    next()
    return;
  }
  if (user) {
    next()
  } else {
    // 防止路由死循环
    if (to.path == "/login") {
      next()
      return;
    }
    next({
      path: "/login"
    })
  }
})

export default router